package com.zz.ecommerce.menu;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 菜单controller
 *
 * @author
 */
@Controller
@RequestMapping("/")
@Scope("prototype")
public class MenuController {
    @Value("${trans.manager.url}")
    private String transManagerUrl;
    @Autowired
    private MenuDao menuDao;

    /**
     * 主页跳转
     *
     * @return
     */
    @RequestMapping(value = "mainPage", method = {RequestMethod.GET, RequestMethod.POST})
    public String mainPage(String flag, String type, String portalName, ModelMap map) {
        map.addAttribute("flag", "124");
        map.addAttribute("type", type);
        map.addAttribute("portalName", "进口B2C(直购)");
        return "mainPage";
    }

    /**
     * 判断当前登陆的用户是会否有登陆的权限
     * vehicle_role_login
     * cargo_role_login
     * ecommerce_role_login
     * portal_role_login
     *
     * @param roleIdentifier
     * @return
     */
    @RequestMapping(value = "/hasRole", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public boolean hasRole(String roleIdentifier) {
        try {
            return SecurityUtils.getSubject().hasRole(roleIdentifier);
        } catch (Exception e) {
            return false;
        }
    }


    /**
     * portal跳转
     *
     * @return
     */
    @RequestMapping(value = "portal", method = {RequestMethod.GET, RequestMethod.POST})
    public String portal(ModelMap modelMap) {
        modelMap.put("transManagerUrl", transManagerUrl);
        return "portal";
    }

    /**
     * 欢迎界面
     *
     * @return
     */
    @RequestMapping(value = "welcome", method = {RequestMethod.GET, RequestMethod.POST})
    public String welcome() {
        return "welcome";
    }

    /**
     * 我的工作台
     *
     * @return
     */
    @RequestMapping(value = "myPlatform", method = {RequestMethod.GET, RequestMethod.POST})
    public String myPlatform(ModelMap modelMap, HttpSession session) {
//        List<Map<String, String>> headerList = menuDao.getHeaderData(SessionUtil.getOrganizationId(session));
//
//        MyPlayformBean myPlayformBean = new MyPlayformBean();
//        if (null != headerList && headerList.size() > 0) {
//            for (Map<String, String> tempMap : headerList) {
//                if (tempMap.get("TYPE").equals("IC")) {
//                    myPlayformBean.setCustImp(tempMap.get("COUNT").toString());
//                } else if (tempMap.get("TYPE").equals("EC")) {
//                    myPlayformBean.setCustExp(tempMap.get("COUNT").toString());
//                } else if (tempMap.get("TYPE").equals("II")) {
//                    myPlayformBean.setInspImp(tempMap.get("COUNT").toString());
//                } else if (tempMap.get("TYPE").equals("OI")) {
//                    myPlayformBean.setInspExp(tempMap.get("COUNT").toString());
//                }
//            }
//        }
//
//        List<Map<String, String>> footerList = menuDao.getFooterData(SessionUtil.getOrganizationId(session));
//        if (null != footerList && footerList.size() > 0) {
//            for (Map<String, String> tempMap : footerList) {
//                if (tempMap.get("TYPE").equals("I")) {
//                    myPlayformBean.setMonImp(tempMap.get("COUNT").toString());
//                } else if (tempMap.get("TYPE").equals("IL")) {
//                    myPlayformBean.setMonLLImp(tempMap.get("COUNT").toString());
//                } else if (tempMap.get("TYPE").equals("O")) {
//                    myPlayformBean.setMonExp(tempMap.get("COUNT").toString());
//                } else if (tempMap.get("TYPE").equals("OL")) {
//                    myPlayformBean.setMonLLExp(tempMap.get("COUNT").toString());
//                }
//            }
//        }
//        int monLLRelease = menuDao.getLLRelease() == 0 ? 1 : menuDao.getLLRelease();
//        myPlayformBean.setRelease(menuDao.getRelease());
//        myPlayformBean.setReleaseLL(menuDao.getRelease() / monLLRelease);
//        modelMap.addAttribute("myPlayformBean", myPlayformBean);
        return "myPlatform";
    }

    /**
     * 查询菜单列表
     *
     * @param flag
     * @return
     */
    @RequestMapping(value = "queryAllMenus/{flag}", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Map queryAllMenus(@PathVariable("flag") String flag) {
        Map<String, Object> re = new HashMap<>();
        List<MenuInfo> menuInfos = menuDao.queryAllMenus(flag);
        //是否成功
        if (null == menuInfos && menuInfos.isEmpty()) {
            re.put("IsOk", "0");
        } else {
            re.put("IsOk", "1");
        }
        Map<String, List> mp = new HashMap<>();
        //菜单信息
        mp.put("menu", menuInfos);
        re.put("ReturnData", mp);
        //菜单字段配置
        re.put("treedefine", getMenuConf());
        return re;
    }

    /**
     * 配置前台menu菜单需要的名称参数
     *
     * @return
     */
    private Map<String, String> getMenuConf() {
        Map<String, String> tree = new HashMap<>();
        tree.put("Indx", "indx");
        tree.put("Caption", "menuName");
        tree.put("Icon", "menuIcon");
        tree.put("Url", "url");
        tree.put("Parent", "pid");
        return tree;
    }

}
